﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;

namespace SurehealthForms
{
    public partial class AdminAddUserForm : Form
    {
        public AdminAddUserForm()
        {
            InitializeComponent();
        }

        public void SaveUser(string filename)
        {
            //try
            //{
            XmlTextWriter Writer;
            Writer = new XmlTextWriter(filename, Encoding.ASCII);
            Writer.Formatting = Formatting.Indented;
            Writer.WriteStartDocument();
            Writer.WriteStartElement("User");
            Writer.WriteAttributeString("Name", textBox1.Text);
            Writer.WriteElementString("AccountType", comboBox1.Text);
            Writer.WriteEndElement();
            Writer.WriteEndDocument();
            Writer.Close();
            //}
            //catch
            //{
            //}
        }

        private void AddUser_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length < 5)//check to make sure they have entered a name
            {
                MessageBox.Show("Your username needs to be over 5 characters");
            }
            else
            {
                if (comboBox1.Text.Length < 1)//check to make sure they selected an account type
                {
                    MessageBox.Show("You need to select a account type");
                }
                else
                {
                    if (safetycheck(textBox1.Text) == true)
                    {
                        SaveUser(textBox1.Text + ".XML");
                        MessageBox.Show("Successfully created Account " + textBox1.Text);
                        textBox1.Text = "";
                        //textBox1.Text = "";
                   
                    }
                    else
                    {
                        MessageBox.Show("Account can only contain characters");
                    }
                }
            }

        }

        private void ExitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //Random rng = new Random();
        //private string passwordGenerator(string Input)
        //{
        //    string Generatedpass="";

        //    int passwordlength = Input.Count()/2;
        //    int switchint;
        //    for (int i = 0; i < passwordlength; i++)
        //    {
        //        switchint = rng.Next(0, 5);
        //        switchint = rng.Next(0, 5);
        //        switchint = rng.Next(0, 5);
        //        switch (switchint)
        //        {
        //            case 0: Generatedpass = Generatedpass + "hd90";
        //                break;
        //            case 1: Generatedpass = Generatedpass + "da2v";
        //                break;
        //            case 2: Generatedpass = Generatedpass + "ht1dx";
        //                break;
        //            case 3: Generatedpass = Generatedpass + "h124f";
        //                break;
        //            default : Generatedpass = Generatedpass + "jfsyh1";
        //              break;
        //        }
        //    }

        //    return Generatedpass;
        //}


        /// <summary>
        /// check wether a string is only consisting off letters
        /// </summary>
        /// <param name="Input">a string you'd like to be checked.</param>
        /// <returns></returns>
        public static bool safetycheck(string Input)
        {
            char[] inputChars = Input.ToCharArray();
            int[] inputToInts = new int[inputChars.Length];
            int i = 0;

            foreach (char c in inputChars)
            {
                inputToInts[i] = (int)c;

                if ((inputToInts[i] > 64 && inputToInts[i] < 91) ||
                    (inputToInts[i] > 96 && inputToInts[i] < 123) || 
                    inputToInts[i] == 32)
                {
                }
                else
                {
                    return false;
                }
                i++;
            }
            return true;
        }

        private void AdminAddUserForm_Load(object sender, EventArgs e)
        {

        }
    }

}
